package com.lizhaoshan.controller;

import com.lizhaoshan.common.Result;
import com.lizhaoshan.dao.SetMealDao;
import com.lizhaoshan.dto.SetMealDto;
import com.lizhaoshan.pojo.PageBean;
import com.lizhaoshan.pojo.SetMeal;
import com.lizhaoshan.service.SetMealService;
import com.lizhaoshan.vo.SetMealVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @program: reggie
 * @description:
 * @author: lamour
 * @create: 2022-05-14 19:54
 **/

@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @PostMapping
    public Result addSetMeal(@RequestBody SetMealDto setMealDto){
        log.info("**********新增套餐*****begin");
        boolean addSetMeal = setMealService.addSetMeal(setMealDto);
        log.info("**********新增套餐*****end");
        return addSetMeal ? Result.success("新增套餐成功") : Result.error("新增套餐失败");
    }

    @PutMapping
    public Result putSetMeal(@RequestBody SetMealDto setMealDto){
        log.info("**********修改套餐*****begin");
        boolean putSetMeal = setMealService.putSetMeal(setMealDto);
        log.info("**********修改套餐*****end");
        return putSetMeal ? Result.success("修改套餐成功") : Result.error("修改套餐失败");
    }

    @DeleteMapping()
    public Result deleteSetMeal(@RequestParam String[] ids){
        log.info("********删除套餐，begin");
        boolean deleteByIds = setMealService.deleteByIds(ids);
        log.info("********删除套餐，end");
        return deleteByIds?Result.success("删除成功"):Result.error("删除失败");
    }

    @GetMapping("/page")
    public Result getSetMealPage(@RequestParam(value = "name", required = false) String name,
                                 @RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "pageSize", required = false) Integer pageSize){
        log.info("**********套餐分页查询*****begin");
        PageBean<SetMealVo> getSetMealPage = setMealService.getSetMealPage(page,pageSize,name);
        log.info("**********套餐分页查询*****end");
        return Objects.nonNull(getSetMealPage) ? Result.success(getSetMealPage) : Result.error("套餐分页查询失败");
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, @RequestParam String[] ids){
        log.info("********修改套餐上下架，begin");
        boolean updateStatus = setMealService.updateStatus(status,ids);
        log.info("********修改套餐上下架，end");
        return updateStatus?Result.success("修改套餐上下架成功"):Result.error("修改套餐上下架失败");

    }


    @GetMapping("{id}")
    public Result getSetMealById(@PathVariable String id){
        log.info("********通过id查询套餐详情，begin");
        SetMealVo getSetMealById = setMealService.getSetMealById(id);
        log.info("********通过id查询套餐详情，end");
        return Objects.nonNull(getSetMealById)?Result.success(getSetMealById):Result.error("通过id查询套餐详情失败");
    }


}
